/*
 * Created by JFormDesigner on Wed May 19 10:47:59 CST 2021
 */

package renthouse.view.sever;

import renthouse.dao.AdminDao;
import renthouse.dao.LandlordDao;
import renthouse.dao.UserDao;
import renthouse.entry.Admin;
import renthouse.entry.Landlord;
import renthouse.entry.User;
import renthouse.util.StringUtil;

import java.awt.*;
import java.awt.event.*;
import java.util.List;
import javax.swing.*;

/**
 * @author Brainrain
 */
public class RegisterFrame extends JFrame {
    public static void main(String[] args) {//注册测试
        RegisterFrame registerFrame = new RegisterFrame();
        registerFrame.setVisible(true);
    }

    public RegisterFrame() {
        initComponents();
    }


    private void buttonRegisterActionPerformed(ActionEvent e) {
        // TODO add your code here
        /*获取表单内的相关信息*/
        String statusVal = (String) status.getSelectedItem();//
        String loginnameVal = loginname.getText();
        String passwordVal = password.getText();
        String password2Val = password2.getText();
        String nameVal = name.getText();
        String sexVal = sex.getText();
        String phoneVal = phone.getText();
        String numberidVal = numberid.getText();
        LoginFrame loginFrame = new LoginFrame();
        /*判断相关的违规信息*/
        if (StringUtil.isNull(loginnameVal)) {
            JOptionPane.showMessageDialog(null, "账户名不能为空");
            return;
        }
        if (StringUtil.isNull(passwordVal)) {
            JOptionPane.showMessageDialog(null, "密码不能为空");
            return;
        }
        if (StringUtil.isNull(password2Val)) {
            JOptionPane.showMessageDialog(null, "确认密码不能为空");
            return;
        }
        if (StringUtil.isNull(numberidVal)) {
            JOptionPane.showMessageDialog(null, "身份证号不能为空");
            return;
        }
        if (StringUtil.isNull(phoneVal)) {
            JOptionPane.showMessageDialog(null, "电话号码不能为空");
            return;
        }
        if (StringUtil.isNull(nameVal)) {
            JOptionPane.showMessageDialog(null, "姓名不能为空");
            return;
        }
        if (StringUtil.isNull(sexVal)) {
            JOptionPane.showMessageDialog(null, "性别不能为空");
            return;
        }
        if (!passwordVal.equals(password2Val)) {
            JOptionPane.showMessageDialog(null, "两次输入的密码不相同");
            return;
        }
        /*管理员*/
        if (statusVal == "管理员") {
            Admin admin = new Admin();
            AdminDao adminDao = new AdminDao();
            //用户名是否重复
            admin.setLoginname(loginnameVal);
            List <Admin> adminList = adminDao.query(admin);

            if (adminList != null && adminList.size() > 0) {
                JOptionPane.showMessageDialog(null, "该用户名已被使用，请换个用户名");
                return;
            }
            //身份号是否重复
            admin = new Admin();
            admin.setNumberid(numberidVal);
            adminList = adminDao.query(admin);
            if (adminList != null && adminList.size() > 0) {
                JOptionPane.showMessageDialog(null, "该身份证号已被注册使用");
                return;
            }
            //电话号码重复
            admin = new Admin();
            admin.setPhone(phoneVal);
            adminList = adminDao.query(admin);
            if (adminList != null && adminList.size() > 0) {
                JOptionPane.showMessageDialog(null, "该电话号码已被注册使用");
                return;
            }
            admin.setLoginname(loginnameVal);
            admin.setPassword(passwordVal);
            admin.setName(nameVal);
            admin.setPhone(phoneVal);
            admin.setSex(sexVal);
            admin.setNumberid(numberidVal);
            boolean f = adminDao.insert(admin);
            if (f) {
                JOptionPane.showMessageDialog(null, "添加成功");
            } else {
                JOptionPane.showMessageDialog(null, "添加失败");
            }
            setVisible(false);
            loginFrame.setVisible(true);
            /*房东*/
        } else if (statusVal == "房东") {
            Landlord landlord = new Landlord();
            LandlordDao landlordDao = new LandlordDao();
            //用户名是否重复
            landlord.setLoginname(loginnameVal);
            List <Landlord> landlordList = landlordDao.query(landlord);

            if (landlordList != null && landlordList.size() > 0) {
                JOptionPane.showMessageDialog(null, "该用户名已被使用，请换个用户名");
                return;
            }
            //身份号是否重复
            landlord = new Landlord();
            landlord.setNumberid(numberidVal);
            landlordList = landlordDao.query(landlord);
            if (landlordList != null && landlordList.size() > 0) {
                JOptionPane.showMessageDialog(null, "该身份证号已被注册使用");
                return;
            }
            //电话号码重复
            landlord = new Landlord();
            landlord.setPhone(phoneVal);
            landlordList = landlordDao.query(landlord);
            if (landlordList != null && landlordList.size() > 0) {
                JOptionPane.showMessageDialog(null, "该电话号码已被注册使用");
                return;
            }
            landlord.setLoginname(loginnameVal);
            landlord.setPassword(passwordVal);
            landlord.setName(nameVal);
            landlord.setPhone(phoneVal);
            landlord.setSex(sexVal);
            landlord.setNumberid(numberidVal);
            boolean f = landlordDao.insert(landlord);
            if (f) {
                JOptionPane.showMessageDialog(null, "添加成功");
            } else {
                JOptionPane.showMessageDialog(null, "添加失败");
            }
            setVisible(false);
            loginFrame.setVisible(true);

            /*房客*/
        } else if (statusVal == "房客") {
            User user = new User();
            UserDao userDao = new UserDao();
            //用户名是否重复
            user.setLoginname(loginnameVal);
            List <User> userList = userDao.query(user);

            if (userList != null && userList.size() > 0) {
                JOptionPane.showMessageDialog(null, "该用户名已被使用，请换个用户名");
                return;
            }
            //身份号是否重复
            user = new User();
            user.setNumberid(numberidVal);
            userList = userDao.query(user);
            if (userList != null && userList.size() > 0) {
                JOptionPane.showMessageDialog(null, "该身份证号已被注册使用");
                return;
            }
            //电话号码重复
            user = new User();
            user.setPhone(phoneVal);
            userList = userDao.query(user);
            if (userList != null && userList.size() > 0) {
                JOptionPane.showMessageDialog(null, "该电话号码已被注册使用");
                return;
            }

            user.setLoginname(loginnameVal);
            user.setPassword(passwordVal);
            user.setName(nameVal);
            user.setPhone(phoneVal);
            user.setSex(sexVal);
            user.setNumberid(numberidVal);
            boolean f = userDao.insert(user);
            if (f) {
                JOptionPane.showMessageDialog(null, "添加成功");
            } else {
                JOptionPane.showMessageDialog(null, "添加失败");
            }
            setVisible(false);
            loginFrame.setVisible(true);


        } else {
            JOptionPane.showMessageDialog(null, "操作错误，请重头开始");
            setVisible(true);
        }


    }

    private void buttonLoginActionPerformed(ActionEvent e) {
        // TODO add your code here
        setVisible(false);
        LoginFrame loginFrame = new LoginFrame();
        loginFrame.setVisible(true);
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        panel1 = new JPanel();
        buttonRegister = new JButton();
        buttonLogin = new JButton();
        panel3 = new JPanel();
        label2 = new JLabel();
        label3 = new JLabel();
        loginname = new JTextField();
        password = new JTextField();
        label4 = new JLabel();
        name = new JTextField();
        label8 = new JLabel();
        sex = new JTextField();
        label9 = new JLabel();
        phone = new JTextField();
        label10 = new JLabel();
        numberid = new JTextField();
        label5 = new JLabel();
        status = new JComboBox <>();
        label11 = new JLabel();
        password2 = new JTextField();
        panel2 = new JPanel();

        //======== this ========
        setTitle("\u6ce8\u518c\u754c\u9762");
        Container contentPane = getContentPane();
        contentPane.setLayout(null);

        //======== panel1 ========
        {
            panel1.setLayout(null);

            //---- buttonRegister ----
            buttonRegister.setText("\u6ce8\u518c");
            buttonRegister.setFont(buttonRegister.getFont().deriveFont(buttonRegister.getFont().getSize() + 6f));
            buttonRegister.setBackground(new Color(204, 204, 204));
            buttonRegister.addActionListener(e -> buttonRegisterActionPerformed(e));
            panel1.add(buttonRegister);
            buttonRegister.setBounds(10, 10, 130, 30);

            //---- buttonLogin ----
            buttonLogin.setText("\u767b\u5165");
            buttonLogin.setFont(buttonLogin.getFont().deriveFont(buttonLogin.getFont().getSize() + 6f));
            buttonLogin.setBackground(new Color(204, 204, 204));
            buttonLogin.addActionListener(e -> buttonLoginActionPerformed(e));
            panel1.add(buttonLogin);
            buttonLogin.setBounds(190, 10, 130, 30);

            { // compute preferred size
                Dimension preferredSize = new Dimension();
                for (int i = 0; i < panel1.getComponentCount(); i++) {
                    Rectangle bounds = panel1.getComponent(i).getBounds();
                    preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                    preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                }
                Insets insets = panel1.getInsets();
                preferredSize.width += insets.right;
                preferredSize.height += insets.bottom;
                panel1.setMinimumSize(preferredSize);
                panel1.setPreferredSize(preferredSize);
            }
        }
        contentPane.add(panel1);
        panel1.setBounds(25, 335, 355, 65);

        //======== panel3 ========
        {
            panel3.setLayout(null);

            //---- label2 ----
            label2.setText("\u8d26\u53f7\uff1a");
            label2.setFont(label2.getFont().deriveFont(label2.getFont().getSize() + 4f));
            panel3.add(label2);
            label2.setBounds(20, 45, 55, label2.getPreferredSize().height);

            //---- label3 ----
            label3.setText("\u5bc6\u7801\uff1a");
            label3.setFont(label3.getFont().deriveFont(label3.getFont().getSize() + 4f));
            panel3.add(label3);
            label3.setBounds(20, 80, 55, 22);
            panel3.add(loginname);
            loginname.setBounds(120, 45, 155, loginname.getPreferredSize().height);
            panel3.add(password);
            password.setBounds(120, 80, 155, 24);

            //---- label4 ----
            label4.setText("\u59d3\u540d\uff1a");
            label4.setFont(label4.getFont().deriveFont(label4.getFont().getSize() + 4f));
            panel3.add(label4);
            label4.setBounds(20, 120, 55, 22);
            panel3.add(name);
            name.setBounds(120, 120, 155, 24);

            //---- label8 ----
            label8.setText("\u6027\u522b\uff1a");
            label8.setFont(label8.getFont().deriveFont(label8.getFont().getSize() + 4f));
            panel3.add(label8);
            label8.setBounds(20, 160, 55, 22);
            panel3.add(sex);
            sex.setBounds(120, 160, 155, 24);

            //---- label9 ----
            label9.setText("\u624b\u673a\u53f7\u7801\uff1a");
            label9.setFont(label9.getFont().deriveFont(label9.getFont().getSize() + 4f));
            panel3.add(label9);
            label9.setBounds(15, 200, 80, 22);
            panel3.add(phone);
            phone.setBounds(120, 200, 155, 24);

            //---- label10 ----
            label10.setText("\u8eab\u4efd\u8bc1\u53f7\u7801\uff1a");
            label10.setFont(label10.getFont().deriveFont(label10.getFont().getSize() + 4f));
            panel3.add(label10);
            label10.setBounds(15, 235, 100, 22);
            panel3.add(numberid);
            numberid.setBounds(120, 235, 155, 24);

            //---- label5 ----
            label5.setText("\u8eab\u4efd\uff1a");
            label5.setFont(label5.getFont().deriveFont(label5.getFont().getSize() + 4f));
            panel3.add(label5);
            label5.setBounds(20, 5, 55, 22);

            //---- status ----
            status.setModel(new DefaultComboBoxModel <>(new String[]{
                    "\u7ba1\u7406\u5458",
                    "\u623f\u4e1c",
                    "\u623f\u5ba2"
            }));
            panel3.add(status);
            status.setBounds(120, 5, 155, 25);

            //---- label11 ----
            label11.setText("\u786e\u8ba4\u5bc6\u7801\uff1a");
            label11.setFont(label11.getFont().deriveFont(label11.getFont().getSize() + 4f));
            panel3.add(label11);
            label11.setBounds(15, 265, 95, 22);
            panel3.add(password2);
            password2.setBounds(120, 265, 155, 24);

            { // compute preferred size
                Dimension preferredSize = new Dimension();
                for (int i = 0; i < panel3.getComponentCount(); i++) {
                    Rectangle bounds = panel3.getComponent(i).getBounds();
                    preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                    preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                }
                Insets insets = panel3.getInsets();
                preferredSize.width += insets.right;
                preferredSize.height += insets.bottom;
                panel3.setMinimumSize(preferredSize);
                panel3.setPreferredSize(preferredSize);
            }
        }
        contentPane.add(panel3);
        panel3.setBounds(20, 15, 355, 315);

        { // compute preferred size
            Dimension preferredSize = new Dimension();
            for (int i = 0; i < contentPane.getComponentCount(); i++) {
                Rectangle bounds = contentPane.getComponent(i).getBounds();
                preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
            }
            Insets insets = contentPane.getInsets();
            preferredSize.width += insets.right;
            preferredSize.height += insets.bottom;
            contentPane.setMinimumSize(preferredSize);
            contentPane.setPreferredSize(preferredSize);
        }
        pack();
        setLocationRelativeTo(getOwner());

        //======== panel2 ========
        {
            panel2.setLayout(new BorderLayout());
        }
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    private JPanel panel1;
    private JButton buttonRegister;
    private JButton buttonLogin;
    private JPanel panel3;
    private JLabel label2;
    private JLabel label3;
    private JTextField loginname;
    private JTextField password;
    private JLabel label4;
    private JTextField name;
    private JLabel label8;
    private JTextField sex;
    private JLabel label9;
    private JTextField phone;
    private JLabel label10;
    private JTextField numberid;
    private JLabel label5;
    private JComboBox <String> status;
    private JLabel label11;
    private JTextField password2;
    private JPanel panel2;
    // JFormDesigner - End of variables declaration  //GEN-END:variables
}
